<?php


namespace app\sys\model;


use think\Model;

class Fun extends Model
{
    protected $autoWriteTimestamp = true;

    public static function add($method, $methodName)
    {
        $row = [
            'method' => $method,
            'method_name' => $methodName,
        ];
        $has = Fun::useGlobalScope(false)
            ->where("method", $method)->find();
        if ($has) {
            $has->isUpdate(true)->save($row);
        } else {
            Fun::create($row);
        }
    }


    public static function list($dutyId, $isData,$isDataStatus): array
    {
        $permsSql = Fun::alias("f")
            ->leftJoin("ic_role_log rl", "rl.fun_id = f.id and rl.duty_id = {$dutyId}")
            ->field("f.id,f.method_name as name,f.is_data,ifnull(rl.data_type,1)as status,if(rl.duty_id = {$dutyId},1,0)as is_role");
        $permsSql = $permsSql->where("f.is_show", 1);
        if ($isData) {
            $permsSql = $permsSql->where("f.is_data", 1);
            $permsSql = $permsSql->where("rl.id", ">", 0);
            $permsSql = $permsSql->where("rl.duty_id", $dutyId);
            $permsSql = $permsSql->where("rl.data_type","in", [$isDataStatus,0]);
        }
        $perms = $permsSql->select()
            ->toArray();
        $tree = [];
        foreach ($perms as $v) {
            $t = explode("/", $v["name"]);
            $k = array_search($t[0], array_column($tree, "label"));

            if ($k === false) {
                $tree[] = ["label" => $t[0], "children" => []];
                $k = array_search($t[0], array_column($tree, "label"));
            }

            if (isset($t[2]) && isset($t[1])) {
                $k2 = array_search($t[1], array_column($tree[$k]["children"], "label"));
                if ($k2 === false) {
                    $tree[$k]["children"][] = ["label" => $t[1], "children" => []];
                    $k2 = array_search($t[1], array_column($tree[$k]["children"], "label"));
                }

                $tree[$k]["children"][$k2]["children"][] = [
                    "id" => $v["id"],
                    "label" => $t[2],
                    "is_role" => $v["is_role"],
                    "is_data" => $v["is_data"],
                    "is_data_status" => $v["status"]
                ];
            } else if (isset($t[1])) {
                $tree[$k]["children"][] = [
                    "id" => $v["id"],
                    "label" => $t[1],
                    "is_role" => $v["is_role"],
                    "is_data" => $v["is_data"],
                    "is_data_status" => $v["status"]
                ];
            }
        }

        return $tree;
    }


    public static function listData($dutyId, $staffNo, $isData,$isDataStatus): array
    {
        $list = [];
        $permsSql = Fun::alias("f")
            ->leftJoin("ic_role_log rl", "rl.fun_id = f.id")
            ->field("rl.fun_id,ifNull(rl.data_type,1)as status")
            ->where("f.is_show", 1);
        $permsSql = $permsSql->where("rl.duty_id", $dutyId);

        if ($isData) {
            $permsSql = $permsSql->where("f.is_data", 1);
            $permsSql = $permsSql->where("rl.id", ">", 0);
            $permsSql = $permsSql->where("rl.duty_id", $dutyId);
            $permsSql = $permsSql->where("rl.data_type","in", [$isDataStatus,0]);
        }


        $perms = $permsSql->select()->toArray();
        foreach ($perms as $k => $v) {
            $d = new class {
                public $fun_id;
                public $status;
            };
            $d->fun_id = $v["fun_id"];
            $d->status = $v["status"];
            $list[] = $d;
        }
        return $list;
    }


    public static function listDataUrl($staffNo, $dutyId): array
    {
        if ($staffNo != "PXS000500") {
            $listSql = RoleLog::alias("rl")
                ->leftJoin("ic_fun f", "f.id = rl.fun_id");
            $listSql = $listSql->where("rl.duty_id", $dutyId)->where("f.is_show", 1);
        } else {
            $listSql = Fun::where("is_show", 1);
        }
        return $listSql->select()->column("method");
    }
}